<template>
  <div class="systemset-city-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="CityID"
        prop="CityID"
        show-overflow-tooltip
        width="200"
      />
      <el-table-column
        align="center"
        label="行政编码"
        prop="CityCode"
        show-overflow-tooltip
        width="200"
      />
      <el-table-column
        align="center"
        label="城市名称"
        prop="CityName"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="是否开放" width="200">
        <template #default="{ row }">
          <el-switch
            v-model="row.nState"
            class="mt-2"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            :active-icon="Check"
            :inactive-icon="Close"
          />
        </template>
      </el-table-column>
    </el-table>
    <div>
      <vab-query-form>
        <vab-query-form-left-panel :span="12">
          <el-button :icon="Plus" type="primary" @click="handleBackup">
            返回省级
          </el-button>
        </vab-query-form-left-panel>
        <vab-query-form-right-panel :span="12">
          <el-pagination
            background
            :current-page="queryForm.pageNo"
            :layout="layout"
            :page-size="queryForm.pageSize"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </vab-query-form-right-panel>
      </vab-query-form>
    </div>
  </div>
</template>

<script>
  import { cityList } from '@/api/systemsetCityarea'
  import { Delete, Plus, Search, Check, Close } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'City',
    setup() {
      const route = useRoute()
      const router = useRouter()
      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          provincecode: 0,
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleBackup = () => {
        router.back()
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await cityList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        state.queryForm.provincecode = route.query.ProvinceCode
        fetchData()
      })
      onUnmounted(() => {
        state.queryForm.provincecode = 0
      })

      return {
        ...toRefs(state),
        setSelectRows,
        handleBackup,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
        Check,
        Close,
      }
    },
  })
</script>
